package com.zimin.cloud.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zimin.cloud.mine.pojo.system.business.AccountBO;
import com.zimin.cloud.mine.pojo.system.view.AccountVO;
import com.zimin.cloud.system.mapper.AccountMapper;
import com.zimin.cloud.system.pojo.entity.Account;
import com.zimin.cloud.system.service.IAccountService;
import com.zimin.common.utils.BeanUtils;
import com.zimin.common.utils.ObjectUtils;
import com.zimin.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * <p>
 * 用户信息 服务实现类
 * </p>
 *
 * @author zimin
 * @since 2021-12-30 11:04
 */
@Service("accountService")
@Slf4j
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements IAccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public AccountVO getAccountById(Serializable accountId) {
        return accountMapper.selectById(accountId, AccountVO.class);
    }

    @Override
    public Page<AccountVO> page(Page<Account> accountPage, AccountBO accountBO) {
        return accountMapper.page(accountPage, queryWrappers(accountBO));
    }

    @Override
    public AccountVO isExistByUserNameAndPwd(AccountBO accountBO) {
        var account = getOne(Wrappers.<Account>lambdaQuery()
                .eq(StringUtils.isNotEmpty(accountBO.getAccountName()), Account::getAccountName, accountBO.getAccountName())
                .eq(StringUtils.isNotEmpty(accountBO.getPassword()), Account::getPassword, accountBO.getPassword()));
        return BeanUtils.coverClass(account, AccountVO.class);
    }

    /**
     * <p>查询wrappers</p>
     *
     * @param accountBO
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.zimin.cloud.system.pojo.entity.Account>
     * @since 2022-02-19 17:12:37
     */
    public QueryWrapper queryWrappers(AccountBO accountBO) {
        var queryWrapper = new QueryWrapper<>();
//        return Wrappers.<Account>lambdaQuery()
        queryWrapper.like(StringUtils.isNotBlank(accountBO.getAccountName()), "account.account_name", accountBO.getAccountName());
        queryWrapper.like(StringUtils.isNotBlank(accountBO.getNickName()), "account.nick_name", accountBO.getNickName());
        queryWrapper.like(StringUtils.isNotBlank(accountBO.getMobile()), "account.mobile", accountBO.getMobile());
        queryWrapper.like(StringUtils.isNotBlank(accountBO.getEmail()), "account.email", accountBO.getEmail());
        queryWrapper.eq(StringUtils.isNotBlank(accountBO.getAccountType()), "account.account_type", accountBO.getAccountType());
        queryWrapper.eq(ObjectUtils.isNotEmpty(accountBO.getStatus()), "account.status", accountBO.getStatus());
        queryWrapper.ge(StringUtils.isNotEmpty(accountBO.getStartCreateTime()), "account.create_time", accountBO.getStartCreateTime());
        queryWrapper.le(StringUtils.isNotEmpty(accountBO.getEndCreateTime()), "account.create_time", accountBO.getEndCreateTime());
        queryWrapper.in(ObjectUtils.isNotEmpty(accountBO.getDeptId()), "dept.id", accountBO.getDeptId());
        queryWrapper.orderByDesc("account.create_time");
        return queryWrapper;
    }
}
